/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package character_9;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import static java.util.Collections.reverseOrder;
import static java.util.Comparator.nullsFirst;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Queue;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Administrator
 */
public class Character_9 {

    /**
     * @param args the command line arguments
     */
    public static void maina(String[] args) throws IOException {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("输入英文");
        Set words = tokenSet(scanner.nextLine());
        System.out.println("不重复的单词" + words.size() + "个" + words );
        
        Set s = new HashSet();
        
        s.add("wo");
        s.add("ai");
        s.add("ai");
        s.add(new Student("张三"));
        s.add(new Student("张三"));//不会去重
        
        System.out.println("" + s.toString());
        System.out.println("" + new Student("张三").hashCode());
        System.out.println("" + new Student("张三").hashCode());
        
        Queue<Request> requests = new LinkedList<>();
        offerRequestTo(requests);
        process(requests);
        
        Stack stack = new Stack(5);
        stack.push("zhangsan");
        stack.push("lisi");
        stack.push("wangwu");
        System.out.println("" + stack.pop());
        System.out.println("" + stack.pop());
        System.out.println("" + stack.pop());
        
        System.out.println("" + new StringComparator().compare("haha", "hehe"));
        
        String[] sb = {"2","1"};
        String sd = Character_9.<String>eleOf(sb, 1);//自动识别类型
        System.out.println("" + Character_9.<String>eleOf(sb, 1) +"||"+sd);
        
        Request re = () -> System.out.println("shiyixia");
        re.execute();
        
        List names = Arrays.asList("zs", "ls", "ww", "ce");//接受不定长自变量，收集为List
//        forEach(names);
//        forEach(new HashSet(names));
//        forEach(new ArrayDeque(names));
        
        List num = Arrays.asList(9,2,3,4,5);
        Collections.sort(num);//由小到大排序
        System.out.println(num);
        
        List accounts = Arrays.asList(new Account("zs", "123", 5000), new Account("ls", "124", 2000), new Account("ww", "125", 3000));
        Collections.sort(accounts);
        System.out.println(accounts);
        
        //操作Comparator,对字母排序
        List ws = Arrays.asList("S", "X", "E");
        Collections.sort(ws);
        System.out.println(ws);
        
        List<String> wds = Arrays.asList("S", "D", "B", "?");
        wds.sort((s1, s2) -> s1.compareTo(s2));//两种方式都可以排序
        System.out.println(wds);
        
        wds.sort(String :: compareTo);//两种方式都可以排序
        System.out.println(wds);
        
        List list = Arrays.asList("A",null,"D");
        list.sort(nullsFirst(reverseOrder()));//null放在最前面
        System.out.println(list);
        
        Map<String, String> messages = new TreeMap<String, String>();//会根据键来排序
        messages.put("301", "zs");
        messages.put("502", "ls");
        messages.put("103", "ww");
        System.out.println(messages);
        
        System.out.println("***************");
        forEachMap(messages.values());
        System.out.println("***************");
        
        Map<String, String> msg = new TreeMap<>((s1, s2) -> -s1.compareTo(s2));//也可以排序
        msg.put("301", "zs");
        msg.put("502", "ls");
        msg.put("103", "ww");
        System.out.println(messages);
        
        Properties p = new Properties();
        p.setProperty("username", "zs");
        p.setProperty("password", "123");
        System.out.println(p.getProperty("username"));
        
        try {
            p.load(new FileInputStream("C:\\Users\\Administrator\\Desktop\\JavaLearningNotes\\src\\character_9\\src\\property\\newproperties.properties"));
            System.out.println(p.getProperty("username"));
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        
        Properties properties = System.getProperties();//获取系统属性
        System.out.println(properties.getProperty("java.version"));
        System.out.println(properties.getProperty("java.class.path"));
        
        msg.keySet().forEach(key -> System.out.println(key));
        
        msg.values().forEach(key -> System.out.println(key));
    
        System.out.println("++++++++");
        //forEach(msg.entrySet());
        
        msg.forEach((key, value) -> System.out.println(key + value));
        
        forEachTest(new HashSet());
        forEachTest(new ArrayList());
        
        System.out.println("+++++++++++");
        Set<Student> students = new TreeSet<>();
//        students.add(new Student("zs"));
//        students.add(new Student("ls"));
        //forEachSet(students);
        
        Set<Integer> numSet = new TreeSet<>();
        numSet.add(1);
        numSet.add(100);
        numSet.add(12);
        forEachInteger(numSet);
        
        Set numbers = new TreeSet();
        numbers.add(1);
        numbers.add(1);
        numbers.add(121);
        numbers.add(89);
        for (Object number : numbers) {
            System.out.println(number);
        }
        
        System.out.println("hashcode:" + new Stack(0).hashCode());
    }
    
    public static void forEachMap(Iterable<String> values){
    
        for (String s1 : values) {
            System.out.println(s1);
        }
    }
    public static void forEachInteger(Collection<Integer> numIntegers){
        for (Integer i : numIntegers) {
            System.out.println(i);
        }
    }
    
    public static void forEachSet(Collection<Student> students){
        for (Student student : students) {
            System.out.println(student.name);
        }
    }
    
    static void forEachTest(Collection i){
        Iterator i1 = i.iterator();
        while (i1.hasNext()) {
            
        }
    }
    
    static void forreach(Iterable<Map.Entry<String, String>> iterable){
        for (Map.Entry<String, String> entry : iterable) {
            System.out.println(entry.getKey() + entry.getValue());
        }
    }
    
    static void forEachOne(Iterable<String> iterable){
        for (String string : iterable) {
            System.out.println(string);
        }
    }
    
//    static void forEach(Iterable iterable){
//        for (Object object : iterable) {
//            System.out.println(object);
//        }
//        System.out.println("=================");
//    }
    
    public static <T> T eleOf(T[] objs, int index){
        
        return objs[index];
    }
    
    public static Set tokenSet(String line){
    
        String[] tokens = line.split(" ");//根据空格切割字符串
        return new HashSet(Arrays.asList(tokens));//使用hashSet实现收集字符串，排除相同的
    }

    private static void offerRequestTo(Queue<Request> requests) {

        for (int i = 0; i < 6; i++) {
//            Request request = new Request() {
//                @Override
//                public void execute() {
//                    System.out.println("data:" + Math.random());
//                }
//            };
//            requests.offer(request);
            requests.offer(() -> System.out.println("data:" + Math.random()));
        }
    }
    
    private static void process(Queue<Request> requests) {
        while (requests.peek() != null) {            
            Request request = (Request) requests.poll();
            request.execute();
        }
    }
 
    
    
}
class Student{

    public String name;

    public Student(String name) {
        this.name = name;
    }
    
    @Override
    public String toString(){
        return name;
    }
     

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 11 * hash + Objects.hashCode(this.name);//根据hash值判断是否为同一对象
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Student other = (Student) obj;
        if (!Objects.equals(this.name, other.name)) {
            return false;
        }
        return true;
    }
}

interface Request{
    void execute();
}

interface Comparator<T>{
    int compare(T o1, T o2);
}

class StringComparator implements Comparator<String>{
    
    @Override
    public int compare(String s1, String s2){
    
        return s1.compareTo(s2);
    }
}
